﻿using UnityEngine;
using System.Collections.Generic;
using LitJson;

/// <summary>
/// 游戏控制器，处理玩家的消息
/// </summary>
public class GameManager : MonoBehaviour {

    //存储所有玩家的字典
    protected Dictionary<string, Player> players = new Dictionary<string, Player>();



    //保存本地玩家引用
    Player localPlayer;

    void Awake()
    {
        EventManager.loginSuccess += OnLogin;
        EventManager.playerMove += PlayerMove;
        EventManager.playerPosition += PlayerPos;
        EventManager.PlayerLogout += OnLogOut;
    }

    private void OnDestroy()
    {
        EventManager.loginSuccess -= OnLogin;
        EventManager.playerMove -= PlayerMove;
        EventManager.playerPosition -= PlayerPos;
        EventManager.PlayerLogout -= OnLogOut;
    }

    /// <summary>
    /// //登录成功回调
    /// </summary>
    /// <param name="json">登录信息</param>
    /// <param name="islocal">是否本地玩家</param>
    void OnLogin(string json, bool islocal)
    {
        UserData user = JsonUtility.FromJson<UserData>(json);
        Player player = CreatePlayer(user);

        //是自己的玩家
        if (islocal)
        {
            GameObject.FindObjectOfType<LocalController>().SetLocol(player);
            localPlayer = player;
        }
        else
        {
            //发送自己的位置给服务器
            localPlayer.userData.pos = localPlayer.transform.position;
            string jsonstr = JsonUtility.ToJson(localPlayer.userData);
            //user.pos = localPlayer.transform.position;//新玩家的位置，不是自己的位置
            NetworkManager.Instance.SendMessage(Protocol.PlayerPosition_CMD, jsonstr);
        }
    }

    void OnLogOut(User user)
    {
        if(players.ContainsKey(user.username))
        {
            //销毁玩家
            Player player = players[user.username];
            Destroy(player.gameObject);

            //删除字典的引用
            players.Remove(user.username);
        }
    }
    /// <summary>
    /// 玩家移动
    /// </summary>
    /// <param name="json">玩家移动数据</param>
    void PlayerMove(MoveData data)
    {
        //矫正玩家的位置和旋转
        players[data.username].Reset(data.position,data.rotation);

        //指定玩家移动
        players[data.username].Move(data.direct);
    }

    /// <summary>
    /// 玩家位置信息
    /// </summary>
    /// <param name="user">玩家数据</param>
    void PlayerPos(UserData user)
    {
        //已存在用户，更新位置
        if (players.ContainsKey(user.username))
        {
            players[user.username].transform.position = user.pos;
        }
        else//不存在玩家，创建玩家
        {
            CreatePlayer(user);
        }
    }

    /// <summary>
    /// 创建一个玩家，绑定Player脚本，存储到players字典中
    /// </summary>
    /// <param name="user">玩家信息</param>
    /// <returns></returns>
    Player CreatePlayer(UserData user)
    {
        GameObject prefab = Resources.Load<GameObject>("Tank");
        //创建游戏对象
        GameObject go = Instantiate(prefab);
        go.transform.position = user.pos;

        Player player = go.GetComponent<Player>();
        player.userData = user;
        player.SetColor(user.color);

        players.Add(user.username, player);
        return player;
    }
}
